package com.xs.tool.method;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xs.tool.ValidateUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class MethodUtil {
    /// 检查参数
    public static void checkParams(Method method,Object[] params) throws IllegalArgumentException{
        Parameter[] parameters = method.getParameters();

        // 参数数量检查
        if (params.length < parameters.length) {
            throw new IllegalArgumentException("参数不足%s".formatted(""));
        }

        // 参数检查
        for (int index = 0; index < parameters.length; index++) {
            Class<?> parameterType = method.getParameterTypes()[index];

            // 获取真实参数
            Object paramValue = params[index];

            // 处理JSONObject
            if (paramValue instanceof JSONObject) {
                paramValue = com.alibaba.fastjson.JSON.toJavaObject((JSONObject) paramValue, parameterType);
            }

            // 处理包装类
            if (parameterType.isPrimitive()) {
                parameterType = switch (parameterType.getName()) {
                    case "int" -> Integer.class;
                    case "long" -> Long.class;
                    case "short" -> Short.class;
                    case "byte" -> Byte.class;
                    case "double" -> Double.class;
                    case "float" -> Float.class;
                    case "boolean" -> Boolean.class;
                    case "char" -> Character.class;
                    default -> parameterType;
                };
            }

            // 处理JSONArray
            if (paramValue instanceof JSONArray jsonArray && parameterType.isArray()) {
                Class<?> componentType = parameterType.getComponentType();
                Object array = java.lang.reflect.Array.newInstance(componentType, jsonArray.size());
                for (int i = 0; i < jsonArray.size(); i++) {
                    Object item = jsonArray.get(i);
                    if (item instanceof JSONObject) {
                        item = com.alibaba.fastjson.JSON.toJavaObject((JSONObject) item, componentType);
                    }
                    java.lang.reflect.Array.set(array, i, item);
                }
                params[index] = array;
                paramValue = array;
            }

            Parameter parameter = parameters[index];

            // 类型检查
            if (paramValue != null && !parameterType.isInstance(paramValue)) {
                String errorString = "参数index:%s,对应方法参数%s,需要%s类型,实际请求为%s类型".formatted(
                        index,
                        parameter.getName(),
                        parameter.getType().getName(),
                        paramValue.getClass().getName()
                );
                throw new IllegalArgumentException(errorString);
            }

            // 注解检查
            Annotation[] annotations = parameter.getAnnotations();
            for (Annotation annotation : annotations) {
                ValidateUtil.validateSingleParam(index, paramValue, annotation);
            }
        }
    }
}
